#!/bin/bash

# MongoShake 服务启动脚本
# 用于管理独立的MongoDB和MongoShake服务

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_message() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查Docker是否运行
check_docker() {
    if ! docker info > /dev/null 2>&1; then
        print_error "Docker is not running. Please start Docker first."
        exit 1
    fi
}

# 启动MongoDB服务
start_mongodb() {
    print_message "Starting MongoDB service..."
    docker-compose -f docker-compose.mongodb.yml up -d
    
    print_message "Waiting for MongoDB to be ready..."
    # 等待MongoDB健康检查通过
    timeout=120
    counter=0
    while [ $counter -lt $timeout ]; do
        if docker-compose -f docker-compose.mongodb.yml ps | grep -q "healthy"; then
            print_message "MongoDB is ready!"
            return 0
        fi
        sleep 2
        counter=$((counter + 2))
        echo -n "."
    done
    
    print_error "MongoDB failed to start within $timeout seconds"
    return 1
}

# 启动MongoShake服务
start_mongoshake() {
    print_message "Starting MongoShake service..."
    docker-compose -f docker-compose.mongoshake.yml up -d
    print_message "MongoShake service started!"
}

# 停止所有服务
stop_all() {
    print_message "Stopping all services..."
    docker-compose -f docker-compose.mongoshake.yml down
    docker-compose -f docker-compose.mongodb.yml down
    print_message "All services stopped!"
}

# 查看服务状态
status() {
    print_message "MongoDB service status:"
    docker-compose -f docker-compose.mongodb.yml ps
    
    echo ""
    print_message "MongoShake service status:"
    docker-compose -f docker-compose.mongoshake.yml ps
}

# 查看日志
logs() {
    if [ "$1" = "mongodb" ]; then
        print_message "MongoDB logs:"
        docker-compose -f docker-compose.mongodb.yml logs -f
    elif [ "$1" = "mongoshake" ]; then
        print_message "MongoShake logs:"
        docker-compose -f docker-compose.mongoshake.yml logs -f
    else
        print_message "All logs:"
        docker-compose -f docker-compose.mongodb.yml logs &
        docker-compose -f docker-compose.mongoshake.yml logs -f
    fi
}

# 清理所有数据
clean() {
    print_warning "This will remove all data volumes. Are you sure? (y/N)"
    read -r response
    if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
        print_message "Cleaning all data..."
        docker-compose -f docker-compose.mongoshake.yml down -v
        docker-compose -f docker-compose.mongodb.yml down -v
        docker network rm mongodb_03-mongoshake_mongodb-network 2>/dev/null || true
        print_message "All data cleaned!"
    else
        print_message "Clean operation cancelled."
    fi
}

# 显示帮助信息
show_help() {
    echo "Usage: $0 [COMMAND]"
    echo ""
    echo "Commands:"
    echo "  start       Start MongoDB and MongoShake services"
    echo "  stop        Stop all services"
    echo "  restart     Restart all services"
    echo "  status      Show service status"
    echo "  logs        Show logs (mongodb|mongoshake)"
    echo "  clean       Remove all data volumes"
    echo "  help        Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0 start                    # Start all services"
    echo "  $0 logs mongoshake          # Show MongoShake logs"
    echo "  $0 status                   # Show all service status"
}

# 主函数
main() {
    check_docker
    
    case "${1:-start}" in
        start)
            start_mongodb
            start_mongoshake
            print_message "All services started successfully!"
            ;;
        stop)
            stop_all
            ;;
        restart)
            stop_all
            sleep 2
            start_mongodb
            start_mongoshake
            print_message "All services restarted successfully!"
            ;;
        status)
            status
            ;;
        logs)
            logs "$2"
            ;;
        clean)
            clean
            ;;
        help|--help|-h)
            show_help
            ;;
        *)
            print_error "Unknown command: $1"
            show_help
            exit 1
            ;;
    esac
}

# 执行主函数
main "$@" 